package com.xuexiang.xqrcode.view;

import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;
import com.google.zxing.ResultPoint;
import com.xuexiang.xqrcode.R;
import com.xuexiang.xqrcode.camera.CameraManager;
import java.util.Collection;
import java.util.HashSet;

/* loaded from: classes2.dex */
public final class ViewfinderView extends View {
    private static final int DEFAULT_ANIMATION_INTERVAL = 25;
    private static final int OPAQUE = 255;
    private int cornerColor;
    private int cornerLength;
    private int cornerWidth;
    private boolean isCircle;
    private Collection<ResultPoint> mLastPossibleResultPoints;
    private int mMaskColor;
    private final Paint mPaint;
    private Collection<ResultPoint> mPossibleResultPoints;
    private Bitmap mResultBitmap;
    private int mResultColor;
    private int mResultPointColor;
    private long scanAnimationInterval;
    private Bitmap scanLight;
    private int scanLineTop;
    private int scanVelocity;

    public ViewfinderView(Context context) {
        this(context, null);
    }

    public ViewfinderView(Context context, AttributeSet attributeSet) {
        this(context, attributeSet, R.attr.ViewfinderViewStyle);
    }

    public ViewfinderView(Context context, AttributeSet attributeSet, int i) {
        super(context, attributeSet, i);
        this.mPaint = new Paint();
        this.mPossibleResultPoints = new HashSet(5);
        initInnerRect(context, attributeSet, i);
    }

    private void initInnerRect(Context context, AttributeSet attributeSet, int i) {
        Resources resources = getResources();
        TypedArray obtainStyledAttributes = context.obtainStyledAttributes(attributeSet, R.styleable.ViewfinderView, i, 0);
        int dimensionPixelSize = obtainStyledAttributes.getDimensionPixelSize(R.styleable.ViewfinderView_inner_marginTop, -1);
        if (dimensionPixelSize != -1) {
            CameraManager.FRAME_MARGIN_TOP = dimensionPixelSize;
        }
        CameraManager.FRAME_WIDTH = obtainStyledAttributes.getDimensionPixelSize(R.styleable.ViewfinderView_inner_width, getDefaultScanSize(getContext()));
        CameraManager.FRAME_HEIGHT = obtainStyledAttributes.getDimensionPixelSize(R.styleable.ViewfinderView_inner_height, getDefaultScanSize(getContext()));
        this.cornerColor = obtainStyledAttributes.getColor(R.styleable.ViewfinderView_inner_corner_color, resources.getColor(R.color.default_inner_corner_color));
        this.cornerLength = obtainStyledAttributes.getDimensionPixelSize(R.styleable.ViewfinderView_inner_corner_length, resources.getDimensionPixelSize(R.dimen.default_inner_corner_length));
        this.cornerWidth = obtainStyledAttributes.getDimensionPixelSize(R.styleable.ViewfinderView_inner_corner_width, resources.getDimensionPixelSize(R.dimen.default_inner_corner_width));
        this.scanLight = BitmapFactory.decodeResource(getResources(), obtainStyledAttributes.getResourceId(R.styleable.ViewfinderView_inner_scan_bitmap, R.drawable.xqrcode_ic_scan_light));
        int color = obtainStyledAttributes.getColor(R.styleable.ViewfinderView_inner_scan_bitmap_tint, -1);
        if (color != -1) {
            Bitmap copy = this.scanLight.copy(Bitmap.Config.ARGB_8888, true);
            this.scanLight.recycle();
            new Canvas(copy).drawColor(color, PorterDuff.Mode.SRC_IN);
            this.scanLight = copy;
        }
        this.scanVelocity = obtainStyledAttributes.getDimensionPixelSize(R.styleable.ViewfinderView_inner_scan_speed, resources.getDimensionPixelSize(R.dimen.default_inner_scan_speed));
        this.scanAnimationInterval = obtainStyledAttributes.getInt(R.styleable.ViewfinderView_inner_scan_animation_interval, 25);
        this.isCircle = obtainStyledAttributes.getBoolean(R.styleable.ViewfinderView_inner_scan_isCircle, true);
        this.mMaskColor = obtainStyledAttributes.getColor(R.styleable.ViewfinderView_inner_mask_color, resources.getColor(R.color.default_mask_color));
        this.mResultColor = obtainStyledAttributes.getColor(R.styleable.ViewfinderView_inner_result_color, resources.getColor(R.color.default_result_color));
        this.mResultPointColor = obtainStyledAttributes.getColor(R.styleable.ViewfinderView_inner_result_point_color, resources.getColor(R.color.default_result_point_color));
        obtainStyledAttributes.recycle();
    }

    @Override // android.view.View
    public void onDraw(Canvas canvas) {
        Rect framingRect = CameraManager.get().getFramingRect();
        if (framingRect == null) {
            return;
        }
        int width = canvas.getWidth();
        int height = canvas.getHeight();
        this.mPaint.setColor(this.mResultBitmap != null ? this.mResultColor : this.mMaskColor);
        float f = width;
        canvas.drawRect(0.0f, 0.0f, f, framingRect.top, this.mPaint);
        canvas.drawRect(0.0f, framingRect.top, framingRect.left, framingRect.bottom, this.mPaint);
        canvas.drawRect(framingRect.right, framingRect.top, f, framingRect.bottom, this.mPaint);
        canvas.drawRect(0.0f, framingRect.bottom, f, height, this.mPaint);
        if (this.mResultBitmap != null) {
            this.mPaint.setAlpha(255);
            canvas.drawBitmap(this.mResultBitmap, framingRect.left, framingRect.top, this.mPaint);
            return;
        }
        drawFrameBounds(canvas, framingRect);
        drawScanLight(canvas, framingRect);
        Collection<ResultPoint> collection = this.mPossibleResultPoints;
        Collection<ResultPoint> collection2 = this.mLastPossibleResultPoints;
        if (collection.isEmpty()) {
            this.mLastPossibleResultPoints = null;
        } else {
            this.mPossibleResultPoints = new HashSet(5);
            this.mLastPossibleResultPoints = collection;
            this.mPaint.setAlpha(255);
            this.mPaint.setColor(this.mResultPointColor);
            if (this.isCircle) {
                for (ResultPoint resultPoint : collection) {
                    canvas.drawCircle(framingRect.left + resultPoint.getX(), framingRect.top + resultPoint.getY(), 6.0f, this.mPaint);
                }
            }
        }
        if (collection2 != null) {
            this.mPaint.setAlpha(127);
            this.mPaint.setColor(this.mResultPointColor);
            if (this.isCircle) {
                for (ResultPoint resultPoint2 : collection2) {
                    canvas.drawCircle(framingRect.left + resultPoint2.getX(), framingRect.top + resultPoint2.getY(), 3.0f, this.mPaint);
                }
            }
        }
        postInvalidateDelayed(this.scanAnimationInterval, framingRect.left, framingRect.top, framingRect.right, framingRect.bottom);
    }

    private void drawScanLight(Canvas canvas, Rect rect) {
        if (this.scanLineTop == 0) {
            this.scanLineTop = rect.top;
        }
        if (this.scanLineTop >= rect.bottom - 30) {
            this.scanLineTop = rect.top;
        } else {
            this.scanLineTop += this.scanVelocity;
        }
        canvas.drawBitmap(this.scanLight, (Rect) null, new Rect(rect.left, this.scanLineTop, rect.right, this.scanLineTop + 30), this.mPaint);
    }

    private void drawFrameBounds(Canvas canvas, Rect rect) {
        this.mPaint.setColor(this.cornerColor);
        this.mPaint.setStyle(Paint.Style.FILL);
        int i = this.cornerWidth;
        int i2 = this.cornerLength;
        canvas.drawRect(rect.left, rect.top, rect.left + i, rect.top + i2, this.mPaint);
        canvas.drawRect(rect.left, rect.top, rect.left + i2, rect.top + i, this.mPaint);
        canvas.drawRect(rect.right - i, rect.top, rect.right, rect.top + i2, this.mPaint);
        canvas.drawRect(rect.right - i2, rect.top, rect.right, rect.top + i, this.mPaint);
        canvas.drawRect(rect.left, rect.bottom - i2, rect.left + i, rect.bottom, this.mPaint);
        canvas.drawRect(rect.left, rect.bottom - i, rect.left + i2, rect.bottom, this.mPaint);
        canvas.drawRect(rect.right - i, rect.bottom - i2, rect.right, rect.bottom, this.mPaint);
        canvas.drawRect(rect.right - i2, rect.bottom - i, rect.right, rect.bottom, this.mPaint);
    }

    public void drawViewfinder() {
        this.mResultBitmap = null;
        invalidate();
    }

    public void addPossibleResultPoint(ResultPoint resultPoint) {
        this.mPossibleResultPoints.add(resultPoint);
    }

    public int getDefaultScanSize(Context context) {
        return (Math.min(getScreenWidth(context), getScreenHeight(context)) * 3) / 4;
    }

    private static int getScreenWidth(Context context) {
        return context.getResources().getDisplayMetrics().widthPixels;
    }

    private static int getScreenHeight(Context context) {
        return context.getResources().getDisplayMetrics().heightPixels;
    }
}
